package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;

    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private IStatementItemService statementItemService;


    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        statement.setStatus(Statement.STATUS_CONSUME);
        statement.setCreateTime(new Date());
        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        Statement temp = this.get(statement.getId());
        if(temp==null){
            throw new BusinessException("参数有误");
        }
        if(Statement.STATUS_PAID.equals(temp.getStatus())){
            throw new BusinessException("已经付款订单无法修改");
        }
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public Statement generateStatementByAppointmentId(Long appointmentId) {
        Appointment appointment = appointmentService.get(appointmentId);
        if (appointment==null){
            throw new BusinessException("参数异常");
        }

        if (Appointment.STATUS_APPOINTMENTS.equals(appointment.getStatus())
                ||Appointment.STATUS_USERCANCELLED.equals(appointment.getStatus())
                ||Appointment.STATUS_TIMEOUTCANCEL.equals(appointment.getStatus())){
            throw new BusinessException("该状态无法生成结算单");
        }

        Statement statement = new Statement();
        statement.setCreateTime(new Date());
        statement.setStatus(Statement.STATUS_CONSUME);
        statement.setAppointmentId(appointmentId);
        statement.setServiceType(appointment.getServiceType());
        statement.setActualArrivalTime(appointment.getActualArrivalTime());
        statement.setCustomerName(appointment.getCustomerName());
        statement.setCustomerPhone(appointment.getCustomerPhone());
        statement.setLicensePlate(appointment.getLicensePlate());
        statement.setCarSeries(appointment.getCarSeries());
        statementMapper.insert(statement);
        return statement;

    }

    @Override
    public Statement queryByAppointmentId(Long appointmentId) {
        if(appointmentId==null){
            throw new BusinessException("参数异常");
        };
        return statementMapper.selectByAppointmentId(appointmentId);
    }

    @Override
    public void updateByIdAndAmount(Long statementId, BigDecimal totalAmount, BigDecimal discountAmount, BigDecimal totalQuantity) {
        statementMapper.updateByIdAndAmount(statementId,totalAmount,discountAmount,totalQuantity);
    }

    @Override
    public void payStatement(Long statementId) {
        statementMapper.changeStatusById(statementId,Statement.STATUS_PAID,new Date(), ShiroUtils.getUserId());
    }

    @Override
    public void remove(Long id) {
        Statement statement = this.get(id);
        if(Statement.STATUS_PAID.equals(statement.getStatus())){
            throw new BusinessException("已经支付订单无法删除");
        }
        statementMapper.deleteByPrimaryKey(id);
        statementItemService.deleteByStatmentId(id);
    }


}
